package cn.iocoder.yudao.module.digital.controller.admin.worksvedio;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.digital.controller.admin.worksvedio.vo.*;
import cn.iocoder.yudao.module.digital.dal.dataobject.worksvedio.WorksVedioDO;
import cn.iocoder.yudao.module.digital.service.worksvedio.WorksVedioService;

@Tag(name = "管理后台 - 数字人视频作品")
@RestController
@RequestMapping("/digital/works-vedio")
@Validated
public class WorksVedioController {

    @Resource
    private WorksVedioService worksVedioService;

    @PostMapping("/create")
    @Operation(summary = "创建数字人视频作品")
    @PreAuthorize("@ss.hasPermission('digital:works-vedio:create')")
    public CommonResult<Long> createWorksVedio(@Valid @RequestBody WorksVedioSaveReqVO createReqVO) {
        return success(worksVedioService.createWorksVedio(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数字人视频作品")
    @PreAuthorize("@ss.hasPermission('digital:works-vedio:update')")
    public CommonResult<Boolean> updateWorksVedio(@Valid @RequestBody WorksVedioSaveReqVO updateReqVO) {
        worksVedioService.updateWorksVedio(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数字人视频作品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('digital:works-vedio:delete')")
    public CommonResult<Boolean> deleteWorksVedio(@RequestParam("id") Long id) {
        worksVedioService.deleteWorksVedio(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数字人视频作品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('digital:works-vedio:query')")
    public CommonResult<WorksVedioRespVO> getWorksVedio(@RequestParam("id") Long id) {
        WorksVedioDO worksVedio = worksVedioService.getWorksVedio(id);
        return success(BeanUtils.toBean(worksVedio, WorksVedioRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数字人视频作品分页")
    @PreAuthorize("@ss.hasPermission('digital:works-vedio:query')")
    public CommonResult<PageResult<WorksVedioRespVO>> getWorksVedioPage(@Valid WorksVedioPageReqVO pageReqVO) {
        PageResult<WorksVedioDO> pageResult = worksVedioService.getWorksVedioPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorksVedioRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数字人视频作品 Excel")
    @PreAuthorize("@ss.hasPermission('digital:works-vedio:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorksVedioExcel(@Valid WorksVedioPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorksVedioDO> list = worksVedioService.getWorksVedioPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数字人视频作品.xls", "数据", WorksVedioRespVO.class,
                        BeanUtils.toBean(list, WorksVedioRespVO.class));
    }

}